{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from RAG.VectorBase import VectorStore\n",
    "from RAG.utils import ReadFiles\n",
    "from RAG.LLM import OpenAIChat, InternLMChat\n",
    "from RAG.Embeddings import JinaEmbedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Calculating embeddings: 100%|██████████| 459/459 [00:04<00:00, 92.38it/s] \n"
     ]
    }
   ],
   "source": [
    "# 建立向量数据库\n",
    "docs = ReadFiles('./data').get_content(max_token_len=600, cover_content=150) # 获得data目录下的所有文件内容并分割\n",
    "vector = VectorStore(docs)\n",
    "embedding = JinaEmbedding(path='/root/autodl-tmp/jinaai/jina-embeddings-v2-base-zh') # 创建EmbeddingModel\n",
    "vector.get_vector(EmbeddingModel=embedding)\n",
    "vector.persist(path='storage') # 将向量和文档内容保存到storage目录下，下次再用就可以直接加载本地的数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chronos:LearningtheLanguageofTimeSeries\n",
      "AbdulFatirAnsari1∗,LorenzoStella1∗,CanerTurkmen1,XiyuanZhang3†,PedroMercado1,\n",
      "HuibinShen1,OleksandrShchur1,SyamaSundarRangapuram1,SebastianPinedaArango4†,\n",
      "ShubhamKapoor1,JasperZschiegner†,DanielleC.Maddix1,HaoWang1,5†,MichaelW.Ma-\n",
      "honey2,6†,KariTorkkola2,AndrewGordonWilson2,7†,MichaelBohlke-Schneider1,Yuyang\n",
      "Wang1{ansarnd,stellalo}@amazon.com\n",
      "1AWSAILabs,2AmazonSupplyChainOptimizationTechnologies,3UCSanDiego,4UniversityofFreiburg,5Rutgers\n",
      "University,6UCBerkeley,7NewYorkUniversity\n",
      "Abstract\n",
      "WeintroduceChronos,asimpleyeteffectiveframeworkforpretrainedprobabilistictime\n",
      "seriesmodels.Chronostokenizestimeseriesvaluesusingscalingandquantizationinto\n",
      "afixedvocabularyandtrainsexistingtransformer-basedlanguagemodelarchitectureson\n",
      "thesetokenizedtimeseriesviathecross-entropyloss.WepretrainedChronosmodels\n",
      "basedontheT5family(rangingfrom20Mto710Mparameters)onalargecollectionof\n",
      "publiclyavailabledatasets,complementedbyasyntheticdatasetthatwegeneratedvia\n",
      "Gaussianprocessestoimprovegeneralization.Inacomprehensivebenchmarkconsistingof\n",
      "42datasets,andcomprisingbothclassicallocalmodelsanddeeplearningmethods,weshow\n",
      "thatChronosmodels:(a)significantlyoutperformothermethodsondatasetsthatwere\n",
      "partofthetrainingcorpus;and(b)havecomparableandoccasionallysuperiorzero-shot\n",
      "performanceonnewdatasets,relativetomethodsthatweretrainedspecificallyonthem.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "vector = VectorStore()\n",
    "\n",
    "vector.load_vector('./storage') # 加载本地的数据库\n",
    "\n",
    "embedding = JinaEmbedding(path='/root/autodl-tmp/jinaai/jina-embeddings-v2-base-zh')\n",
    "\n",
    "question = 'chronos是什么？'\n",
    "\n",
    "content = vector.query(question, EmbeddingModel=embedding, k=1)[0]\n",
    "\n",
    "print(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "de27a17e84bb48e8ae3df85e001b6458",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/8 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据提供的上下文，Chronos是一个用于预训练概率时间序列模型的简单而有效的框架。它使用缩放和量化将时间序列值转换为固定词汇表，并使用这些tokenized时间序列训练现有的Transformer-based语言模型架构。Chronos模型基于T5家族（从20M到710M参数）在大量公开可用的数据集上进行了预训练，并结合了一个合成数据集，以改善泛化能力。\n",
      "\n",
      "在包含42个数据集的全面基准测试中，Chronos模型在训练数据集中表现出色，并且在新的数据集上具有零样本性能，与专门针对这些数据集训练的方法相比，其性能有时甚至更优。\n",
      "\n",
      "综上所述，Chronos是一个用于预训练概率时间序列模型的框架，旨在提高时间序列处理的性能和泛化能力。\n"
     ]
    }
   ],
   "source": [
    "model = InternLMChat(path='/root/autodl-tmp/Shanghai_AI_Laboratory/internlm2-chat-7b')\n",
    "print(model.chat(question, [], content))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
